package cn.org.itool.web.config;

import cn.org.itool.web.interceptor.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;

/**
 * @author 祥霸
 * @version 1.0
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Value("${i-tool.auth.web-config.exclude-path:}")
    private List<String> excludePath;

    @Value("${i-tool.auth.web-config.exclude-path-auth:}")
    private List<String> excludePathAuth;

    private static final List<String> EXCLUDE_PATH_ = Arrays.asList("/js/**", "/css/**", "/images/**");

    @Bean
    public PreLoginInterceptor getPreLoginInterceptor() {
        return new PreLoginInterceptor();
    }

    @Bean
    public CROSInterceptor getCROSInterceptor() {
        return new CROSInterceptor();
    }


    @Bean
    public WapLoginInterceptor getWapLoginInterceptor() {
        return new WapLoginInterceptor();
    }

    @Bean
    public WapAjaxAuthInterceptor getWapAjaxAuthInterceptor() {
        return new WapAjaxAuthInterceptor();
    }

    @Bean
    public ThreadLocalInterceptor getThreadLocalInterceptor() {
        return new ThreadLocalInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getPreLoginInterceptor()).addPathPatterns("/**")
                .excludePathPatterns(this.excludePath);
        registry.addInterceptor(getCROSInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(getWapLoginInterceptor()).addPathPatterns("/**")
                .excludePathPatterns(this.excludePath);
        registry.addInterceptor(getWapAjaxAuthInterceptor()).addPathPatterns("/**")
                .excludePathPatterns(this.excludePathAuth);
        registry.addInterceptor(getThreadLocalInterceptor()).addPathPatterns("/**")
                .excludePathPatterns(EXCLUDE_PATH_);
    }
}
